import FormService from '../apis/form';
import {Message} from 'iview';
//import {handleFormRule} from 'xinya-table';

const SET_FORM_FIELD = 'SET_FORM_FIELD';
const SET_FORM = 'SET_FORM';

const state = {
    formRepository: {},
    formFieldRepository: {},
};
const getters = {};
const actions = {
    loadForm({commit, state}, payload) {
        return FormService.getFormApi(payload.formId).then(res => {
            if (res.data.code !== 0) {
                Message.error(res.data.msg);
                throw new Error(res.data.msg);
            } else {
                return res.data.data;
            }
        }).then(data => {
            let rule = data;//handleFormRule(data);
            commit(SET_FORM_FIELD, {
                id: payload.formId,
                list: rule
            });
            commit(SET_FORM, {
                formId: payload.formId,
                list: data
            });
            if (payload.success != undefined) {
                payload.success();
            }
        }).catch(err => {
            console.log(err);
        });
    }
};
const mutations = {
    [SET_FORM_FIELD](state, formData) {
        state.formRepository[formData.id] = formData.list;
    },
    [SET_FORM](state, {formId, list}) {
        state.formFieldRepository[formId] = list;
    }
};

export default {
    namespaced: true,
    state,
    getters,
    actions,
    mutations
};